package com.cms.image.action;

import java.util.ArrayList;
import java.util.List;

import com.cms.image.model.Image;
import com.cms.image.service.ImageService;
import com.jframe.sys.controller.ExtendAction;
import com.jframe.sys.controller.utils.DicCache;
import com.jframe.sys.controller.utils.JsBuilder;
import com.jframe.sys.utils.CalendarUtil;
import com.jframe.sys.utils.JsonUtil;
import com.jframe.sys.utils.SpringUtil;
import com.jframe.sys.utils.StringUtil;
import com.jframe.sys.webui.Columns;
import com.jframe.sys.webui.Editor;
import com.jframe.sys.webui.Grid;

public class ImageAction extends ExtendAction {
	private static String ACTION="imageAction";
	private static String SERVICE = "imageService";
	private static Class<Image> CLAZZ = Image.class;
	public void list(){
		List<Columns> frozenColumns = new ArrayList<Columns>();// 固定列
		List<Columns> commonColumns = new ArrayList<Columns>();// 活动列
		
		frozenColumns.add(new Columns("ck", "", 40, ""));
		frozenColumns.add(new Columns("操作", 120, "operation"));
		frozenColumns.add(new Columns("id", "编号", 100, ""));
		frozenColumns.add(new Columns("title", "图片标题", 200, ""));
		commonColumns.add(new Columns("typeId", "图片类别", 120, new Editor("combobox",true),DicCache.getComboboxJson("sm_cms_imageType")));
		commonColumns.add(new Columns("uploadTime", "上传时间", 120, ""));
		commonColumns.add(new Columns("author", "图片作者", 120, ""));
		commonColumns.add(new Columns("width", "图片宽度", 120, ""));
		commonColumns.add(new Columns("height", "图片高度", 120, ""));	
		commonColumns.add(new Columns("visitNum", "访问人数", 120, ""));
		commonColumns.add(new Columns("loveNum", "图片喜爱人数", 120, ""));
		
		Grid grid = new Grid();
		grid.setUrl(this.getUrl(ACTION+".do?method=load"));
		String js = JsBuilder.getJsStr(frozenColumns, commonColumns, grid);
		this.request.setAttribute("js", js);
		this.forward("/cms/image/list/imageList.jsp");
	}
	
	public void load(){
		baseLoad(SERVICE, CLAZZ);
	}
	
	public void save() {
		baseSave(SERVICE, CLAZZ);
	}
	
	public Object beforeSave(Object obj){
		Image image = (Image)obj;
		if(image.getStatus()==0){
			image.setStatus(1);
		}
		if(StringUtil.isStringNull(image.getUploadTime())){
			image.setUploadTime(CalendarUtil.getDateTime());
		}
		return image;
	}

	public void delete() {
		baseDelete(SERVICE);
	}

	public void getObjById() {
		this.print(JsonUtil.obj2Json(this.getObject(SERVICE)));
	}
	
	public void setStatus(){
		String ids = request.getParameter("ids");
		int status = StringUtil.parseInt(request.getParameter("status"));
		ImageService service = (ImageService)SpringUtil.getBean(SERVICE);
		service.setStatus(ids, status);
		this.print(true);
	}
}
